package com.caration.shell;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

import com.caration.shell.cr3d.SharedData;

public class AlertHelper {

	public static void showToast(int resId) {
		showToast(SharedData.getContext().getString(resId));
	}

	public static void showToast(final String string) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				Toast.makeText(SharedData.getContext(), string,
						Toast.LENGTH_SHORT).show();
			}
		};

		SharedData.getGLHandler().post(r);
	}

	public static void showDialog(int messageId, Runnable positiveRun) {
		showDialog(messageId, positiveRun, null);
	}

	public static void showDialog(int messageId, Runnable positiveRun,
			Runnable negativeRun) {
		showDialog(messageId, R.string.public_action_ok,
				R.string.public_action_cancel, positiveRun, negativeRun);
	}

	public static void showDialog(int messageId, int positiveStrId,
			int negativeStrId, Runnable positiveRun, Runnable negativeRun) {
		showDialog(SharedData.getContext(), messageId, positiveStrId,
				negativeStrId, positiveRun, negativeRun);
	}

	public static void showDialog(Context context, int messageId,
			int positiveStrId, int negativeStrId, Runnable positiveRun,
			Runnable negativeRun) {
		showDialog(context, context.getString(messageId),
				context.getString(positiveStrId),
				context.getString(negativeStrId), positiveRun, negativeRun);
	}

	public static void showDialog(Context context, String message,
			String positiveStr, String negativeStr, final Runnable positiveRun,
			final Runnable negativeRun) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context)
				.setMessage(message).setCancelable(false)
				.setPositiveButton(positiveStr, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (positiveRun != null)
							positiveRun.run();
					}
				}).setNegativeButton(negativeStr, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (negativeRun != null)
							negativeRun.run();
					}
				});

		Runnable r = new Runnable() {
			@Override
			public void run() {
				builder.create().show();
			}
		};

		if (SharedData.getGLHandler() != null) {
			SharedData.getGLHandler().post(r);
		} else {
			r.run();
		}
	}
}
